package com.keyinong.caizhengting.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.ImageUtilsTouxiang;
import com.keyinong.caizhengting.tools.NetWorkUtil;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author：郑谦
 * @时间：2017-4-10 下午4:02:27
 * @类名：MyinfoActivity.java
 * @功能: 个人中心-我的信息
 */

public class MyinfoActivity extends Activity {
    HttpTools httptools;
    /**
     * 姓名
     */
    private TextView tv_name;
    /**
     * 手机号
     */
    private TextView tv_shouji;
    /**
     * 身高
     */
    private EditText et_shengao;
    /**
     * 体重
     */
    private EditText et_tizhong;
    /**
     * 提交
     */
    private Button btn_tijiao;
    private ImageButton ib_fanhui;
    private RelativeLayout rl_shengao;
    private RelativeLayout rl_tizhong;
    /**
     * 头像
     */
    private ImageView iv_touxiang;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//			getWindow().addFlags(
//					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_myinfo);
        initRes();
    }


    private void initRes() {
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_shouji = (TextView) findViewById(R.id.tv_shouji);
        et_shengao = (EditText) findViewById(R.id.et_shengao);
        et_tizhong = (EditText) findViewById(R.id.et_tizhong);
        btn_tijiao = (Button) findViewById(R.id.btn_submit);
        ib_fanhui = (ImageButton) findViewById(R.id.ib_fanhui);
        rl_shengao = (RelativeLayout) findViewById(R.id.rl_shengao);
        rl_tizhong = (RelativeLayout) findViewById(R.id.rl_tizhong);
        iv_touxiang = (ImageView) findViewById(R.id.iv_touxiang);
        ib_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Personal();
        et_shengao.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                btn_tijiao.setVisibility(View.VISIBLE);
                et_shengao.setCursorVisible(true);
                et_shengao.setText("");
            }
        });
        et_tizhong.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                btn_tijiao.setVisibility(View.VISIBLE);
                et_tizhong.setCursorVisible(true);
                et_tizhong.setText("");
            }
        });
//		et_shengao.setCursorVisible(false);
//		et_tizhong.setCursorVisible(false);
        btn_tijiao.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
//				btn_tijiao.setVisibility(View.VISIBLE);
                Save();
            }
        });
    }

    /**
     * 保存身高体重
     */
    private void Save() {
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
        String height = et_shengao.getText().toString();
        String weigth = et_tizhong.getText().toString();
        if (height.equals("")) {
            Toast.makeText(MyinfoActivity.this, "请填写身高", Toast.LENGTH_SHORT).show();
            return;
        }
        if (weigth.equals("")) {
            Toast.makeText(MyinfoActivity.this, "请填写体重", Toast.LENGTH_SHORT).show();
            return;
        }
        httptools = new HttpTools(MyinfoActivity.this);
        httptools.updateInfo(token, height, weigth, new JsonHttpResponseHandler() {
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                // TODO Auto-generated method stub
                super.onSuccess(statusCode, headers, response);
                try {
                    //							Log.i("zq",response.toString());
                    boolean code = response.getBoolean("code");
                    String msg = response.getString("msg");
                    if (code == true) {
                        Toast.makeText(MyinfoActivity.this, msg, Toast.LENGTH_SHORT).show();
                        Personal();
                    } else {
                        Toast.makeText(MyinfoActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 加载个人信息
     */
    private void Personal() {
        if (NetWorkUtil.isNetwork(getApplicationContext()) == false) {
            Toast.makeText(getApplicationContext(), "当前网络不可用", Toast.LENGTH_SHORT).show();
            return;
        }
//		infolist=new ArrayList<PersonalEntity>();
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
//		Log.i("zq", "token---"+token);
        if (httptools == null) {
            httptools = new HttpTools(MyinfoActivity.this);
        }
        httptools.person(token, new JsonPerson());
    }

    public class JsonPerson extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
                Log.i("zq", response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    JSONObject object = response.getJSONObject("data");
                    String name = object.getString("name");
                    String username = object.getString("username");
                    String logo = object.getString("logo");
                    String height = object.getString("height");
                    String weight = object.getString("weight");
                    tv_name.setText(name);
                    tv_shouji.setText(username);
                    et_tizhong.setText(weight);
                    et_shengao.setText(height);
                    ImageUtilsTouxiang.displayImage(MyinfoActivity.this, logo, iv_touxiang, R.drawable.touxiang1);
                } else {
                    Toast.makeText(MyinfoActivity.this, msg, Toast.LENGTH_SHORT).show();
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
